package inseniarlingua.download

import groovy.transform.CompileStatic;
import groovy.transform.TypeCheckingMode;

@CompileStatic
class Eng2InaDictDownload {
	static final URL_PREFIX = 'http://www.interlingua.com/an/ceid-english'
	static final String LETTERS = 'abcdefghijklmnopqrstuvwxyz'
	static final File TARGET_DIR_ENG = new File("dictionaries/eng")
	static final File TARGET_DIR_ENG_INA = new File("${TARGET_DIR_ENG}/ina")
	
	void download(List<URL> urls) {
		if (!TARGET_DIR_ENG_INA.exists()) {
			assert TARGET_DIR_ENG_INA.mkdirs()
		}
		for (url in urls) {
			downloadUrl(url)
		}
	}
	
	void downloadUrl(URL url) {
		final text = url.text
//		println text.substring(0, 30)
		final letter = url.file[-1]
		final file = new File("${TARGET_DIR_ENG_INA}/${letter}.html")
		println "file=$file"
		file.text = text
	}
	
	@CompileStatic(TypeCheckingMode.SKIP)
	List<URL> getUrls() {
		final ret = []
		for (i in 0..<LETTERS.length()) {
			final ch = LETTERS[i]
			ret << new URL("${URL_PREFIX}$ch")
		}	
		return ret
	}
	
	static void main(String[] args) {
		final inst = new Eng2InaDictDownload()
		final urls = inst.getUrls()
		urls.each { println it }
		//inst.download(urls)
		final url = new URL("http://www.wordfrequency.info/free.asp?s=y")
		//println url.text
		File target = new File("${TARGET_DIR_ENG}/1-grams-5000.html")
		target.text = url.text
	}

}
